module(...,package.seeall)
-----------------------------------------------------------------------------------------
--
-- menu.lua
--
-----------------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local physics = require( "physics" )
physics.start()
 
-- include Corona's "widget" library
local widget = require "widget"
local gameConstant = require "GameConstant";

local mapGame = require "mapGame";
-- Set the default volume to 50%
media.setSoundVolume(0.5)
--------------------------------------------

-- forward declarations and other locals
local playBtn
local onlineBtn
local tutorialBtn
local optionBtn
local baseline = 2*display.contentHeight/3
--local baseline = 280
local timeMoveButton = 200;
local timeMoveToButton = 200;

-- audio  
local audioGame  = require ("audioGame");
if gameConstant.statusSound then
    audioGame.statusBackground = audio.play( audioGame.backgroundAudio, { channel=1, loops=-1, fadein=500 } )
end

mapGame.building1 = display.newImage( "images/Building3.png" )
mapGame.building2 = display.newImage( "images/Building2.png" )
mapGame.building3 = display.newImage( "images/Building1.png" )
mapGame.building4 = display.newImage( "images/Building3.png" )
mapGame.building5 = display.newImage( "images/Building2.png" )
mapGame.building6 = display.newImage( "images/Building1.png" )

mapGame.building1:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building1.width = display.contentWidth/3
mapGame.building1.x = 0
--print (display.contentHeight )
mapGame.building1.y = display.contentHeight

mapGame.building2:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building2.width = display.contentWidth/3
mapGame.building2.x = display.contentWidth/3
mapGame.building2.y = display.contentHeight

mapGame.building3:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building3.width = display.contentWidth/3
mapGame.building3.x = 2*display.contentWidth/3
mapGame.building3.y = display.contentHeight

mapGame.building4:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building4.width = display.contentWidth/3
mapGame.building4.x = display.contentWidth
mapGame.building4.y = display.contentHeight

mapGame.building5:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building5.width = display.contentWidth/3
mapGame.building5.x = 4*display.contentWidth/3
mapGame.building5.y = display.contentHeight

mapGame.building6:setReferencePoint( display.BottomLeftReferencePoint )
mapGame.building6.width = display.contentWidth/3
mapGame.building6.x = 5*display.contentWidth/3
mapGame.building6.y = display.contentHeight

mapGame.tree[1] = display.newImage( "images/obstack.png" )
mapGame.tree[1]:setReferencePoint( display.CenterReferencePoint )
mapGame.tree[1].contentWidth = 34;
mapGame.tree[1].contentHeight = 34;
mapGame.tree[1].x = 20; 
mapGame.tree[1].y = baseline - gameConstant.heightGrass - 30;
mapGame.tree[1].dx = gameConstant.speed

mapGame.tree[2] = display.newImage( "images/obstack.png")
mapGame.tree[2]:setReferencePoint( display.TopLeftReferencePoint )
mapGame.tree[2].contentWidth = 34;
mapGame.tree[2].contentHeight = 34;
mapGame.tree[2].x = 200; 
mapGame.tree[2].y = baseline - gameConstant.heightGrass;
mapGame.tree[2].dx = gameConstant.speed

for i = 1, #mapGame.tree, 1 do
    mapGame.tree[i].tag = "obstack";
    physics.addBody( mapGame.tree[i], "static", { density = 1.0, friction = 0.5, bounce = 0.2 } )
end
-- Add way

mapGame.grass = display.newRect(0, 0, display.contentWidth, gameConstant.heightGrass );
mapGame.grass:setReferencePoint( display.TopLeftReferencePoint );
mapGame.grass:setStrokeColor(0, 0, 0)
mapGame.grass:setFillColor(0, 0, 0)
mapGame.grass.x = 0
mapGame.grass.y = baseline 
mapGame.grass.tag = "way"
physics.addBody( mapGame.grass, "static", { density = 1.0, friction = 0.5, bounce = 0.2 } )
-- 'onRelease' event listener for playBtn
local function onPlayBtnRelease()
	playBtn.xScale = 1; playBtn.yScale = 1;
	storyboard.gotoScene( "mapPlay", "fade", 100 )
	
	return true	-- indicates successful touch
 end

-- 'onPress' event listener for playBtn
local function onPlayBtnPress( )
        if gameConstant.statusSound then 
            audio.play( audioGame.buttonClick)
        end
        playBtn.xScale = 1.5; playBtn.yScale = 1.5;
	return true	-- indicates successful touch
end
 -- listenerOnlineBtn 
local listenerPlayBtn = function(obj)
    transition.to( playBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=display.contentHeight-60} )
end   
-- onRelease event listener for onlineBtn 
local function onOnlineBtnRelease( )
	playBtn.xScale = 1; playBtn.yScale = 1;
	storyboard.gotoScene( "level1", "fade", 100 )
	return true	-- indicates successful touch
end
-- 'onPress' event listener for onlineBtn
local function onOnlineBtnPress( )
        if gameConstant.statusSound then 
            audio.play( audioGame.buttonClick)
        end
	onlineBtn.xScale = 1.5; onlineBtn.yScale = 1.5;
	return true	-- indicates successful touch
end
-- listenerOnlineBtn 
local listenerOnlineBtn = function(obj)
    transition.to( onlineBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=display.contentHeight-240} )
end 
-- onRelease event listener for optionBtn 
local function onOptionBtnRelease()
	optionBtn.xScale = 1; optionBtn.yScale = 1;
	-- go to level1.lua scene
	local menu = storyboard.loadScene( "option" )
	storyboard.gotoScene( "option", "fade", 100 )
	return true	-- indicates successful touch
    end
-- 'onPress' event listener for optionBtn
local function onOptionBtnPress( )
        if gameConstant.statusSound then 
            audio.play( audioGame.buttonClick)
        end
	optionBtn.xScale = 1.5; optionBtn.yScale = 1.5;
	return true	-- indicates successful touch
    end
-- listenerOnlineBtn 
local listenerOptionBtn = function(obj)
    transition.to( optionBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=display.contentHeight-160} )
end
-- onRelease event listener for tutorialBtn 
local function onTutorialBtnRelease()
	tutorialBtn.xScale = 1;tutorialBtn.yScale = 1;
        --storyboard.remov
        local menu = storyboard.loadScene( "tutorial" )
	storyboard.gotoScene( "tutorial", "fade", 100 )
	return true	-- indicates successful touch
end
-- 'onPress' event listener for tutorialBtn 
local function onTutorialBtnPress()
        if gameConstant.statusSound then 
            audio.play( audioGame.buttonClick)
        end
	tutorialBtn.xScale = 1.5;tutorialBtn.yScale = 1.5;
	return true	-- indicates successful touch
end    
-- listenerOnlineBtn 
local listenerTutorialBtn = function(obj)
    transition.to( tutorialBtn, { time = timeMoveToButton, alpha=1.0,x = display.contentWidth*0.5, y=display.contentHeight-200} )
end


local tPrevious = system.getTimer()

--Function move map 
local function moveMap(event)
        local tDelta = event.time - tPrevious
        tPrevious = event.time
	local xOffset = ( gameConstant.speedBuilding * tDelta )
	mapGame.building1.x = mapGame.building1.x - xOffset
	mapGame.building2.x = mapGame.building2.x - xOffset
        mapGame.building3.x = mapGame.building3.x - xOffset
	mapGame.building4.x = mapGame.building4.x - xOffset
        mapGame.building5.x = mapGame.building5.x - xOffset
	mapGame.building6.x = mapGame.building6.x - xOffset
	
	if (mapGame.building1.x + mapGame.building1.contentWidth) < 0 then
                mapGame.building1.x = mapGame.building6.x + mapGame.building6.contentWidth
        end
            
	if (mapGame.building2.x + mapGame.building2.contentWidth) < 0 then
                mapGame.building2.x = mapGame.building1.x + mapGame.building1.contentWidth
        end
        if (mapGame.building3.x + mapGame.building3.contentWidth) < 0 then
                mapGame.building3.x = mapGame.building2.x + mapGame.building2.contentWidth
        end
        if (mapGame.building4.x + mapGame.building4.contentWidth) < 0 then
                mapGame.building4.x = mapGame.building3.x + mapGame.building3.contentWidth
        end
        if (mapGame.building5.x + mapGame.building5.contentWidth) < 0 then
                mapGame.building5.x = mapGame.building4.x + mapGame.building4.contentWidth
        end
        if (mapGame.building6.x + mapGame.building6.contentWidth) < 0 then
                mapGame.building6.x = mapGame.building5.x + mapGame.building5.contentWidth
        end
        xOffset = ( gameConstant.speed * tDelta )
	local i
	for i = 1, #mapGame.tree, 1 do
		mapGame.tree[i].x = mapGame.tree[i].x - mapGame.tree[i].dx * tDelta * gameConstant.speed
		if (mapGame.tree[i].x + mapGame.tree[i].contentWidth) < 0 then
			mapGame.tree[i]:translate( 480 + mapGame.tree[i].contentWidth * 2, 0 )
		end
	end
end



-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--		 unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
	local group = self.view

	-- display a background image
	     
 
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	local group = self.view
	-- INSERT code here (e.g. start timers, load audio, start listeners, etc.)
	local background = display.newImageRect( "images/menu/background.png", display.contentWidth, display.contentHeight )
        --local background = display.newImage( "images/menu/backgroundCity.png", true )
	background:setReferencePoint( display.TopLeftReferencePoint )
	background.x, background.y = 0, 0
	
	-- create/position logo/title image on upper-half of the screen
	local titleLogo = display.newImage( "images/menu/lr_logo.png" )
	titleLogo:setReferencePoint( display.CenterReferencePoint )
        titleLogo.x = display.contentWidth/2
	titleLogo.y = 40
	
    
	-- create a widget button (which will loads level1.lua on release)
	playBtn = widget.newButton{
		label="",
		labelColor = { default={255}, over={128} },
		default="images/menu/lr_menu_start.png",
		width=154, height=30,
                onPress = onPlayBtnPress,
		onRelease = onPlayBtnRelease	-- event listener function
	}
	playBtn:setReferencePoint( display.CenterReferencePoint )
	playBtn.x = display.contentWidth*0.5
	playBtn.y = display.contentHeight - 20
        transition.from( playBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerPlayBtn } )
        -- Create button option
        optionBtn = widget.newButton{
		label="",
		labelColor = { default={255}, over={128} },
		default="images/menu/lr_menu_options.png",
		width=154, height=30,
                onPress =onOptionBtnPress,
		onRelease = onOptionBtnRelease 	-- event listener function
	}
        transition.from( backBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerBackBtn } )
	optionBtn:setReferencePoint( display.CenterReferencePoint )
	optionBtn.x = display.contentWidth*0.5
	optionBtn.y = display.contentHeight - 120
        transition.from( optionBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerOptionBtn } )
        -- create button tutorial
        tutorialBtn  = widget.newButton{
		--label="Tutorial",
		labelColor = { default={255}, over={128} },
		default="images/menu/lr_menu_tutorial.png",
		width=154, height=30,
                onPress = onTutorialBtnPress,
		onRelease = onTutorialBtnRelease 	-- event listener function
	}
	tutorialBtn:setReferencePoint( display.CenterReferencePoint )
	tutorialBtn.x = display.contentWidth*0.5
	tutorialBtn.y = display.contentHeight - 160
        transition.from( tutorialBtn, { time = timeMoveButton,  x=display.contentWidth/2, y=0, onComplete=listenerTutorialBtn } )
        -- create button online
        onlineBtn  = widget.newButton{
		--label="Online",
		labelColor = { default={255}, over={128} },
		default="images/menu/lr_menu_online.png",
		width=154, height=30,
                onPress = onOnlineBtnPress,
		onRelease = onOnlineBtnRelease -- event listener function
	}
	onlineBtn:setReferencePoint( display.CenterReferencePoint )
	onlineBtn.x = display.contentWidth*0.5
	onlineBtn.y = display.contentHeight - 200
        transition.from( onlineBtn, { time = timeMoveButton,  x=display.contentWidth*0.5, y=0, onComplete=listenerOnlineBtn } )
        
	
	-- all display objects must be inserted into group
	
        group:insert( background )
        --group:insert( graveSprite )
        group:insert( titleLogo )
        group:insert( optionBtn )
        group:insert( tutorialBtn )
        group:insert( onlineBtn )
	group:insert( playBtn ) 
end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
        print ("hehe");
	-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
	if playBtn then
		playBtn:removeSelf()	-- widgets must be manually removed
		playBtn = nil
	
        end 
        if onlineBtn then
		onlineBtn:removeSelf()	-- widgets must be manually removed
		onlineBtn = nil
       end
       if tutorialBtn then
		tutorialBtn:removeSelf()	-- widgets must be manually removed
		tutorialBtn = nil
       end
       if optionBtn then
		optionBtn:removeSelf()	-- widgets must be manually removed
		optionBtn = nil
            end
       if background then
           background:removeSelf( )
           background = nil
       end 
             
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
	local group = self.view
	print("remove cac nut bam");
	                                                               
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------
Runtime:addEventListener( "enterFrame", moveMap );
return scene